// send_and_check_bank_data.js
// 发送银行流水数据并检查处理结果

const axios = require('axios');

// 银行流水数据（来自拦截的数据）
const bankData = {
  "batchId": "batch_test_" + Date.now(),
  "timestamp": new Date().toISOString(),
  "dataType": "bank",
  "data": [{
    "url": "/uc-server/service/hzDealDetailReport/hzDealDetailQuery",
    "method": "POST",
    "timestamp": new Date().toISOString(),
    "response": {
      "success": true,
      "errorCode": "0",
      "msg": "成功",
      "data": {
        "messageTypeId": "0000",
        "transactionCode": "CCM02001",
        "receiverNode": "",
        "receiverServer": "",
        "receiverJournalNo": "20251124EA000000004754266604",
        "receiverDate": "2025-11-24",
        "receiverTime": "09:53",
        "transactionRef": "00262025112487262643",
        "destinationChannel": "",
        "businessCycle": 0,
        "businessDate": "",
        "relatedCycleNo": 0,
        "relatedAccDate": "",
        "statusCode": "000000",
        "statusType": "",
        "statusDescription": "",
        "hostName": "ueccashserver5",
        "threadNo": "231",
        "sourceSys": "",
        "targetSys": "",
        "systemLevel": "",
        "transDesc": "",
        "state": "N",
        "files": [],
        "filesMap": null,
        "checkToken": "",
        "responseFlowNo": "2025112405482447",
        "currentPage": 1,
        "pageSize": 50,
        "totalPageNumber": 1,
        "totalNumber": 3,
        "data": [
          {
            "orderFlowNo": "20251121R9989895",
            "debitAmount": "80994.00",
            "loanAmount": "0.00",
            "balance": "168666.06",
            "currencyType": "00",
            "currencyTypeHZ": "人民币",
            "currencyType1": "00",
            "currencyType1HZ": "人民币",
            "transferDate": "2025-11-21",
            "transferTime": "17:39:02",
            "summaryNo": "V5",
            "summary": "汇兑",
            "vouchType": "2025-11-21",
            "vouchersNo": "",
            "recAccount": "50131000425516134",
            "recAccountName": "上海致渊贸易有限公司",
            "recAccountOpenBank": "上海农商银行江川支行",
            "tranFee": "0.00",
            "transferNode": "",
            "transferNodeHZ": "",
            "organName": "上海农商银行江川支行",
            "memo": "货款",
            "hostFlowNo": "202511217358723715",
            "recAccountOpenNode": "",
            "recBankType": "01",
            "bocFlag": "0",
            "busTime": "",
            "channelCode": "0026",
            "subAccountSeq": ""
          },
          {
            "orderFlowNo": "20251121R8686971",
            "debitAmount": "160.00",
            "loanAmount": "0.00",
            "balance": "249660.06",
            "currencyType": "00",
            "currencyTypeHZ": "人民币",
            "currencyType1": "00",
            "currencyType1HZ": "人民币",
            "transferDate": "2025-11-21",
            "transferTime": "17:22:13",
            "summaryNo": "V5",
            "summary": "汇兑",
            "vouchType": "2025-11-21",
            "vouchersNo": "",
            "recAccount": "8888888732129349",
            "recAccountName": "永康市小呱子贸易有限公司",
            "recAccountOpenBank": "浙江网商银行股份有限公司",
            "tranFee": "0.00",
            "transferNode": "",
            "transferNodeHZ": "",
            "organName": "浙江网商银行股份有限公司",
            "memo": "货款",
            "hostFlowNo": "202511217358647590",
            "recAccountOpenNode": "",
            "recBankType": "01",
            "bocFlag": "0",
            "busTime": "",
            "channelCode": "0026",
            "subAccountSeq": ""
          },
          {
            "orderFlowNo": "20251121R8699081",
            "debitAmount": "1610.00",
            "loanAmount": "0.00",
            "balance": "249820.06",
            "currencyType": "00",
            "currencyTypeHZ": "人民币",
            "currencyType1": "00",
            "currencyType1HZ": "人民币",
            "transferDate": "2025-11-21",
            "transferTime": "17:21:32",
            "summaryNo": "V5",
            "summary": "汇兑",
            "vouchType": "2025-11-21",
            "vouchersNo": "",
            "recAccount": "817972401421004626",
            "recAccountName": "高新开发区德研仕医疗器械商行",
            "recAccountOpenBank": "威海银行股份有限公司",
            "tranFee": "0.00",
            "transferNode": "",
            "transferNodeHZ": "",
            "organName": "威海银行股份有限公司",
            "memo": "货款",
            "hostFlowNo": "202511217358731484",
            "recAccountOpenNode": "",
            "recBankType": "01",
            "bocFlag": "0",
            "busTime": "",
            "channelCode": "0026",
            "subAccountSeq": ""
          }
        ],
        "accountNo": "3301040160020881390",
        "accountName": "杭州银行三墩支行",
        "accAlias": "",
        "flag": "0",
        "pageTotal": "3",
        "debitTotal": "82764.00",
        "loanTotal": "0.00",
        "debitPageTotal": "82764.00",
        "loanPageTotal": "0.00",
        "subAccountSeq": "",
        "inTotal": "0",
        "outTotal": "3",
        "complete": true,
        "timestamp": "1763949213773",
        "success": true,
        "code": "000000",
        "message": ""
      }
    },
    "status": 200
  }]
};

// 发送数据到API
async function sendBankData() {
  try {
    console.log('🚀 正在发送银行流水数据到API...');
    console.log('📊 数据条数:', bankData.data.length);
    
    // 发送数据到API (使用正确的端口和路径)
    const response = await axios.post('http://127.0.0.1:9001/api/v1/plugin/data/receive', bankData);
    
    console.log('✅ 数据发送成功!');
    console.log('📥 API响应:', response.data);
    
    return response.data;
  } catch (error) {
    console.error('❌ 发送数据失败:', error.message);
    if (error.response) {
      console.error('📋 错误详情:', error.response.data);
    }
    throw error;
  }
}

// 查询处理状态
async function checkProcessingStatus(batchId) {
  try {
    console.log('🔍 查询数据处理状态...');
    
    const response = await axios.get(`http://127.0.0.1:9001/api/v1/plugin/data/status?batchId=${batchId}`);
    
    console.log('📊 处理状态:', response.data);
    return response.data;
  } catch (error) {
    console.error('❌ 查询处理状态失败:', error.message);
    if (error.response) {
      console.error('📋 错误详情:', error.response.data);
    }
    throw error;
  }
}

// 查询批次列表
async function checkBatches() {
  try {
    console.log('🔍 查询批次列表...');
    
    const response = await axios.get('http://127.0.0.1:9001/api/v1/plugin/data/batches');
    
    console.log('📋 批次列表:', response.data);
    return response.data;
  } catch (error) {
    console.error('❌ 查询批次列表失败:', error.message);
    if (error.response) {
      console.error('📋 错误详情:', error.response.data);
    }
    throw error;
  }
}

// 主函数
async function main() {
  try {
    console.log('🚀 开始测试银行流水数据处理...');
    
    // 1. 发送银行流水数据
    const result = await sendBankData();
    
    // 2. 等待一段时间让后台处理
    console.log('⏳ 等待数据处理...');
    await new Promise(resolve => setTimeout(resolve, 3000));
    
    // 3. 查询处理状态
    if (result && result.data && result.data.batchId) {
      await checkProcessingStatus(result.data.batchId);
    }
    
    // 4. 查询批次列表
    await checkBatches();
    
    console.log('✅ 测试完成!');
  } catch (error) {
    console.error('❌ 测试失败:', error.message);
    process.exit(1);
  }
}

// 执行主函数
if (require.main === module) {
  main();
}

module.exports = {
  bankData,
  sendBankData,
  checkProcessingStatus,
  checkBatches
};